' TarApp.vb
'
' Example application written in VB.NET that uses the Tar.dll ,
' defined in Tar.cs .
'
' ------------------------------------------------------------------
'
' Copyright (c) 2009 by Dino Chiesa
' All rights reserved!
'
' ------------------------------------------------------------------
'
' This program and the library it uses is licensed under the Microsoft
' Public License (Ms-PL) See the accompanying License.txt file for
' details.
'

Imports System
Imports System.Collections.Generic
Imports Ionic


Namespace Ionic.SmallApplications

    Public Class TarApp

        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal args As String())
            If (args.Length = 0) Then
                TarApp.Usage
            End If
            Me._options = New Tar.Options()
            Me.ProcessOptionString(IIf((args(0).Chars(0) = "-"c), args(0).Substring(1), args(0)))
            Dim i As Integer
            For i = 1 To args.Length - 1
                If (args(i) = "-?") Then
                    TarApp.Usage
                End If
                If Me._expectFile Then
                    If Me._fileNames Is Nothing Then
                        Me._fileNames = New List(Of String)
                        Me._archiveName = args(i)
                    Else
                        Me._fileNames.Add(args(i))
                    End If
                End If
            Next i

            If Me._action = New TarAction(AddressOf Me.CreateArchive) Then
                If (Me._fileNames.Count = 0) Then
                    Throw New ArgumentException("You must specify one of more filenames to add to the archive.")
                End If
            End If

            If (Me._action Is Nothing) Then
                Throw New ArgumentException
            End If
        End Sub

        Private Sub CreateArchive()
            Ionic.Tar.CreateArchive(Me._archiveName, Me._fileNames, Me._options)
        End Sub

        Private Sub ExtractArchive()
            Ionic.Tar.Extract(Me._archiveName, Me._options)
        End Sub

        Private Sub ListContents()
            Dim list As System.Collections.ObjectModel.ReadOnlyCollection(Of Ionic.Tar.TarEntry) = _
                Ionic.Tar.List(Me._archiveName)
            Dim s As Ionic.Tar.TarEntry
            For Each s In list
                If Me._verbose Then
                    Console.WriteLine("{0,-20}  {1,9}  {2}", s.Mtime.ToString("u"), s.Size, s.Name)
                Else
                    Console.WriteLine("{0}", s.Name)
                End If
            Next
            If Me._verbose Then
                Console.WriteLine(New String("-"c, &H42))
                Console.WriteLine("                                 {0} entries", list.Count)
            End If
        End Sub


        Public Shared Sub Main(ByVal args As String())
            Try
                Dim x As New TarApp(args)
                x.Run
            Catch exc1 As Exception
                Console.WriteLine("Exception: {0}", exc1.ToString)
                TarApp.Usage
            End Try
        End Sub



        Private Sub ProcessOptionString(ByVal arg As String)
            Dim c As Char
            For Each c In arg.ToCharArray
                Select Case c
                    Case "c"c
                        If (Not Me._action Is Nothing) Then
                            Throw New ArgumentException("c")
                        End If
                        Me._action = New TarAction(AddressOf Me.CreateArchive)

                    Case "t"c
                        If (Not Me._action Is Nothing) Then
                            Throw New ArgumentException("t")
                        End If
                        Me._action = New TarAction(AddressOf Me.ListContents)

                    Case "x"c
                        If (Not Me._action Is Nothing) Then
                            Throw New ArgumentException("x")
                        End If
                        Me._action = New TarAction(AddressOf Me.ExtractArchive)

                    Case "v"c
                        Me._verbose = True
                        Me._options.StatusWriter = Console.Out

                    Case "f"c
                        If Me._expectFile Then
                            Throw New ArgumentException("f")
                        End If
                        Me._expectFile = True

                    Case "z"c
                        If (Me._options.Compression <> Tar.TarCompression.None) Then
                            Throw New ArgumentException("z")
                        End If
                        Me._options.Compression = Tar.TarCompression.GZip

                End Select
            Next
        End Sub



        Public Sub Run()
            Me._action.Invoke
        End Sub



        Public Shared Sub Usage()
            Console.WriteLine(ChrW(10) & _
                              "Tar: process tar archives." & ChrW(10) & ChrW(10) & _
                              "Usage:" & ChrW(10) & _
                              "  Tar [options] [tarfile]" & ChrW(10) & _
                              "Examples:" & ChrW(10) & ChrW(10) & _
                              "   to list the entries in a tar archive:" & ChrW(10) & _
                              "     tar -tvf  archive.tar" & ChrW(10) & ChrW(10) & _
                              "   to create a tar archive:" & ChrW(10) & _
                              "     tar -cvf newarchive.tar dir1 file1 .." & ChrW(10) & _
                              "   to create a gzip-compressed tar archive:" & ChrW(10) & _
                              "     tar -cvfz newarchive.tar dir1 file1 .." & ChrW(10))
            Environment.Exit(1)
        End Sub


        ' Fields
        Private _action As TarAction
        Private _expectFile As Boolean
        Private _fileNames As List(Of String)
        Private _archiveName As String
        Private _verbose As Boolean
        Private _options As Tar.Options


        ' Nested Types
        Private Delegate Sub TarAction()

    End Class


End Namespace
